<?php


namespace app\admin\controller\User;


use app\admin\controller\BaseController;
use app\admin\model\User\PartnerModel;
use app\admin\model\User\UserApplyPartnerModel;
use app\admin\model\User\UserModel;
use app\Request;
use app\Utils\ImageUtil;
use app\Utils\Util;

class UserApplyPartner extends BaseController
{
    /**
     * 合伙人信息
     * @param Request $request
     * @return array
     */
    public function getPartnerInfo(Request $request)
    {
        $partner = PartnerModel::getPartnerWhere([['id','=',1]]);
        if(!empty($partner)){
            $partner['details'] = !empty($partner['details']) ? htmlspecialchars_decode($partner['details']) : "";
            $partner['img'] = ImageUtil::i()->replaceUrl($partner['img']);
            $images = !empty($partner['slider_img']) ? json_decode($partner['slider_img'],true) : [];
            foreach ($images as &$image){
                $image = ImageUtil::i()->replaceUrl($image);
            }
            $partner['slider_img'] = $images;
        }else{
            $partner = [
                'partner_name'=>"",
                'img'=>"",
                'slider_img'=>[],
                'details'=>"",
                'price'=>"",
                'vip_price'=>"",
                'cost_price'=>"",
            ];
        }
        return Util::returnJson(self::CODE_DATA,$partner);
    }

    /**
     * 编辑
     * @param Request $request
     * @return array
     */
    public function editPartner(Request $request)
    {
        $param = $request->param();

        $data = [
            'partner_name'=>!empty($param['partner_name']) ? $param['partner_name'] : "",
            'details'=>!empty($param['details']) ? htmlspecialchars($param['details']) : "",
            'price'=>!empty($param['price']) ? $param['price'] : 0,
            'vip_price'=>!empty($param['vip_price']) ? $param['vip_price'] : 0,
            'cost_price'=>!empty($param['cost_price']) ? $param['cost_price'] : 0,
            'up_time'=>date("Y-m-d H:i:s",time()),
        ];

        //图片上传(单图)
        if (!empty($request->file('file'))) {
            $file = $request->file('file');
            $path = "/partner/img/";
            $url = ImageUtil::i()->upload($file, $path);
        }
        //多图
        if (!empty($request->file('files'))) {
            $files = $request->file('files');
            $path = "/partner/slider_img/";
            $urls = ImageUtil::i()->upload($files, $path);
        }

        if (!empty($url)) {
            $data['img'] = $url;
        }
        if (!empty($urls)) {
            $data['slider_img'] = json_encode($urls);
        }

        if(empty($param['id'])){
            $data['add_time'] = date("Y-m-d H:i:s",time());
            $id = PartnerModel::addPartner($data);
            if($id){
                return Util::returnJson(self::CODE_SUCCESS,null,"修改成功");
            }else{
                return Util::returnJson(self::CODE_ERROR,null,"修改失败");
            }
        }else{
            $flag = PartnerModel::updatePartnerWhere([['id','=',$param['id']]],$data);
            if($flag){
                return Util::returnJson(self::CODE_SUCCESS,null,"修改成功");
            }else{
                return Util::returnJson(self::CODE_ERROR,null,"修改失败");
            }
        }
    }

    /**
     * 分页查询
     * @param Request $request
     * @return array
     */
    public function getUserApplyPartnerList(Request $request)
    {
        $param = $request->param();
        $where = [];
        $page = !empty($param['page']) ? $param['page'] : 1;
        $limit = !empty($param['limit']) ? $param['limit'] : 10;
        $status = !empty($param['status']) ? $param['status'] : "";
        if (!empty($status)) {
            if ($status == 1) {
                array_push($where, ['uap.status', '=', 0]);
            }
            if ($status == 2) {
                array_push($where, ['uap.status', '=', 1]);
            }
            if ($status == -1) {
                array_push($where, ['uap.status', '=', -1]);
            }
        }
        $list = UserApplyPartnerModel::getUserApplyPartnerListWhere($page, $limit, $where,'uap.*,u.account');
        $count = UserApplyPartnerModel::getUserApplyPartnerCountWhere($where);
        return Util::returnPageJson(self::CODE_DATA, $count, $list, "");

    }

    /**
     * 处理成功
     * @param Request $request
     * @return array
     */
    public function disposeUserApplyPartner(Request $request)
    {
        $param = $request->param();
        $id = !empty($param['id']) ? $param['id'] : 0;
        $status = !empty($param['status']) ? $param['status'] : 0;
        $partner = UserApplyPartnerModel::getUserApplyPartnerWhere([['id','=',$id]]);
        if(!empty($status)){
            if($partner['status'] == 0){
                if($status['status'] == 1){
                    //处理通过
                    $save = [
                        'status'=>1,
                        'up_time'=>date("Y-m-d H:i:s",time())
                    ];
                    //更新用户信息
                    $user_save = [
                        'real_name'=>$partner['real_name'],
                        'is_partner'=>1
                    ];
                    UserApplyPartnerModel::updateUserApplyPartnerWhere([['id','=',$partner['id']]],$save);
                    UserModel::updateUserWhere([['id','=',$partner['uid']]],$user_save);
                    //赠送会员激活码

                    return Util::returnJson(self::CODE_SUCCESS,null,"处理成功");
                }else{
                    //拒绝
                    $save = [
                        'status'=>-1,
                        'up_time'=>date("Y-m-d H:i:s",time())
                    ];
                    UserApplyPartnerModel::updateUserApplyPartnerWhere([['id','=',$partner['id']]],$save);
                    return Util::returnJson(self::CODE_SUCCESS,null,"处理成功");
                }
            }else{
                return Util::returnJson(self::CODE_ERROR,null,"申请已处理");
            }
        }else{
            return Util::returnJson(self::CODE_ERROR,null,"处理失败");
        }
    }

}